/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.cezary.butler.walili.server.db.tasks;
import pl.cezary.butler.walili.common.User;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * HSQL DB COMPAT
 * @author Przemysław Bałdyga
 */
public class GetUserTask extends FetchingTask<User>{

    private String LOGIN;
    private String HASLO;
    int idUser;
    
    private static final Logger log = Logger.getLogger("ZadanieIsUser");    

    public GetUserTask(int idUser) {
        this.idUser = idUser;
    }
    
    @Override
    public void executeInternal() {
        try {
            String st = "SELECT login,haslo FROM users WHERE id=" + idUser;
            Statement que = null;
            Connection c=getDS().getConnection();
            que = c.createStatement();
            ResultSet res=que.executeQuery(st);
            boolean isEmpty=true;
            boolean isMoreThanOne=false;
            while(res.next()){
                if(isEmpty){
                    isEmpty=false;
                LOGIN=res.getString("login");
                HASLO=res.getString("haslo");
                }
                else isMoreThanOne=true;
                
            }
            que.close();
            if(isMoreThanOne) log.log(Level.SEVERE, "Wielu u\u017cytkownik\u00f3w id:{0}", idUser);
        } catch (SQLException ex) {
            log.log(Level.SEVERE, "Nie udało sie rozłączyć z bazą danych");
            //getIdFactory().fail();
        }
    }

    @Override
    protected User $getResultsNonLocking() {
        return new User(LOGIN,HASLO);
    }

}
